package file

import (
	"end/config"
	"end/httpEntry"
	"github.com/gin-gonic/gin"
	"net/http"
)

func UploadFile(r *gin.Engine) {
	r.POST("/upload/file", func(c *gin.Context) {
		// 从请求中获取文件
		file, err := c.FormFile("file")
		if err != nil {
			c.JSON(http.StatusOK, httpEntry.BaseResponse{
				Code:  10010,
				Error: httpEntry.ERROR10010,
			})
			return
		}

		// 生成保存文件的路径
		savePath := "./data/" + file.Filename

		// 保存文件到本地
		err = c.SaveUploadedFile(file, savePath)
		if err != nil {
			c.JSON(http.StatusOK, httpEntry.BaseResponse{
				Code:  10010,
				Error: httpEntry.ERROR10010,
			})
			return
		}
		uploadFile := config.UploadFile(savePath, file.Filename)
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"url":     uploadFile,
			"message": "上传成功",
		})
	})

}
